API Corner - Manipulating the MainWin

level: intermediate

by Alyce Watson [http://alycesrestaurant.com/]

Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index


Why manipulate the mainwin?

There are several useful commands that are recognized by the MainWin such as the MAINWIN command that allows you to set the size according to the number of rows and columns of text, or the TITLEBAR command which allows you to change the text on the titlebar. There are some things that can only be accomplished by API calls. For instance, you cannot close the MainWin once it is opened. See Tip Corner - the NoMainwin Command.

If the MainWin is not suppressed with the NOMAINWIN command, it is the first window displayed at the start of the program. If you use the API call to GetActiveWindow at the start of your code, you can retrieve the handle of the MainWin and use this handle to manipulate the window with API calls. This was first discussed in issue #53, when Liberty BASIC was a 16-bit language. We're updating the information here for Liberty BASIC 4, which is a 32-bit language. A 32-bit language uses API calls with a slightly different syntax than a 16-bit language.

Getting the handle of the MainWin.

To retrieve the handle of the MainWin, put this code at the top of a program:

 
CallDLL #user32, "GetActiveWindow",_
hMainwin As uLong    'returns handle of mainwin

Setting the size and position of the MainWin.

The MoveWindow API call sets the location and dimensions of the MainWin. You might want to use this so that the window is sized according to pixels, instead of rows and columns of text as it is in the MAINWIN command. You might also want to locate the window at a specific spot on the desktop. Here's how to set the size and position of the MainWin:

 
CallDLL #user32, "GetActiveWindow",_
hMainwin As uLong    'returns handle of MainWin

'can size window with MAINWIN command
'or can size AND locate with API:

x=10 : y=10 : w=230 : h = 150
CallDLL #user32, "MoveWindow",_
    hMainwin As uLong,_ 'handle
    x As Long,_         'x location
    y As Long,_         'y location
    w As Long,_         'width
    h As Long,_         'height
    1 As Boolean,_      'repaint, 1=true
    r As Boolean        'nonzero=success

Closing the MainWin.

There is no Liberty BASIC command to close the MainWin without ending the program with an END command. If you want to close the MainWin, but not end the program, you can do it with the DestroyWindow API call, like this:

'close MainWin without ending program
Calldll #user32, "DestroyWindow",_
    hMainwin as uLong,_
    result as boolean   'nonzero=success

A demo that uses these methods.

The following little demo retrieves the handle of the MainWin when the program opens. It sets the size and position of the MainWin. The user is given a message. In this case, it is just a message to hit the ENTER key to continue with the program. After the user hits ENTER, the MainWin is closed and a GUI program window opens.

 
CallDLL #user32, "GetActiveWindow",_
hMainwin As uLong    'returns handle of MainWin

'can size window with MAINWIN command
'or can size AND locate with API:

x=10 : y=10 : w=230 : h = 150
CallDLL #user32, "MoveWindow",_
    hMainwin As uLong,_ 'handle
    x As Long,_         'x location
    y As Long,_         'y location
    w As Long,_         'width
    h As Long,_         'height
    1 As Boolean,_      'repaint, 1=true
    r As Boolean        'nonzero=success


print "Hit ENTER to continue."
input aVar$

'close MainWin without ending program
Calldll #user32, "DestroyWindow",_
    hMainwin as uLong,_
    result as boolean   'nonzero=success
      
'the MainWin is now closed
'open a GUI window and continue program
button #1, "Close Me",[quit],UL,10,10      
Open "A GUI Window" for window as #1
#1 "trapclose [quit]"
wait
[quit]  close #1:end

Setting the way the MainWin is displayed.

The ShowWindow API call allows you to open the MainWin maximized, minimized, or even hidden. It requires the handle of the MainWin and a flag to set the "show window" state. This little demo opens the MainWin maximized on the user's desktop.

CallDLL #user32, "GetActiveWindow",_
hMainwin As uLong    'returns handle of MainWin

'open the MainWin maximized
    'SW_HIDE = 0
    'SW_NORMAL = 1
    'SW_SHOWMINIMIZED = 2
    'SW_MAXIMIZE = 3
    'SW_SHOWNOACTIVATE = 4
    'SW_SHOW = 5
    'SW_MINIMIZE = 6
    'SW_SHOWMINNOACTIVE = 7
    'SW_SHOWNA = 8
    'SW_RESTORE = 9
    flag = _SW_MAXIMIZE
    CallDLL #user32, "ShowWindow",_
    hMainwin As uLong,_ 'handle
    flag As Long,_		'the ShowWindow state
    r As Boolean

print "Hit ENTER to quit."
input aVar$

'close MainWin 
Calldll #user32, "DestroyWindow",_
    hMainwin as uLong,_
    result as boolean   'nonzero=success

END

A login demo.

This might be a good way to get user login information. You could give the user a message such as "Please enter password." Your program would then evaluate the input. If the password entered was valid, the program would continue, otherwise it would end.

CallDLL #user32, "GetActiveWindow",_
hMainwin As uLong    'returns handle of MainWin

'can size window with MainWin command
'or can size AND locate with API:

x=10 : y=10 : w=230 : h = 150
CallDLL #user32, "MoveWindow",_
    hMainwin As uLong,_ 'handle
    x As Long,_         'x location
    y As Long,_         'y location
    w As Long,_         'width
    h As Long,_         'height
    1 As Boolean,_      'repaint, 1=true
    r As Boolean        'nonzero=success


print "Please enter password."
print "(It's 'hello'.)"
input pswrd$

'close MainWin without ending program
Calldll #user32, "DestroyWindow",_
    hMainwin as uLong,_
    result as boolean   'nonzero=success

'if password was wrong, end program
if lower$(pswrd$) <> "hello" then end

'the MainWin is now closed
'open a GUI window and continue program
button #1, "Close Me",[quit],UL,10,10
Open "A GUI Window" for window as #1
#1 "trapclose [quit]"
wait
[quit]  close #1:end


Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index